package drds.configuration.configuration;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Properties;

/**
 * @author caozhenghui@drds.team<330937205@qq.com>
 */
public class ConfigUtil {
    public static String slash = "/";
    public static String separator = null;
    //
    public static Mode mode = Mode.DEVELOP;
    public static Class classPathOuterBaseClass;


    public static void setClassPathOuterBaseClass(Class aClass) {
        synchronized (ConfigUtil.class) {
            if (classPathOuterBaseClass != null) {
                throw new IllegalArgumentException("不要重复设置");
            }
            classPathOuterBaseClass = aClass;
        }
    }

    public static ClassLoader getClassPathOuterBaseClassLoader() {
        if (classPathOuterBaseClass == null) {
            throw new IllegalArgumentException("请先设置项目class path outer class ");
        }
        return classPathOuterBaseClass.getClassLoader();
    }

    /**
     * /WEB-INF/classes
     */
    public static String getClassPath() {
        String rootPath = getClassPathOuterBaseClassLoader().getResource("").getPath();
        String classFolderPath = rootPath;// rootPath.replace("classes/", "classes");
        classFolderPath = classFolderPath.substring(0, classFolderPath.lastIndexOf("classes/")) + "classes";
        //
        /**
         * 跟目录在这替换
         */
        if (System.getProperty("os.name").toLowerCase().contains("windows")) {
            classFolderPath = classFolderPath.substring(1).replace(slash, separator());
        }
        return classFolderPath;
    }

    /**
     * /WEB-INF/classes/configuration
     */
    public static String getClassPathConfigPath() {
        return getClassPath() + separator() + "configuration";
    }

    /**
     * /WEB-INF/classes/common/develop
     */
    public static String getDevelopConfigPath() {
        return getClassPathConfigPath() + separator() + "develop";
    }

    /**
     * /WEB-INF/classes/common/test
     */
    public static String getTestConfigPath() {
        return getClassPathConfigPath() + separator() + "test";
    }

    /**
     * /WEB-INF/classes/common/produce
     */
    public static String getProduceConfigPath() {
        return getClassPathConfigPath() + separator() + "produce";
    }

    public static Mode getMode() {
        try {
            File modePropertiesFile = new File(getClassPathOuterBaseClassLoader().getResource("common/mode.properties").toURI());
            Properties properties = load(modePropertiesFile);
            String mode = properties.getProperty("mode");
            if ("develop".equals(mode)) {
                return Mode.DEVELOP;
            } else if ("produce".equals(mode)) {
                return Mode.PRODUCE;
            } else if ("test".equals(mode)) {
                return Mode.TEST;
            } else {
                throw new RuntimeException("不支持");
            }
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 读取属性文件
     */
    public static Properties load(File file) {
        Properties properties = new Properties();
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            properties.load(fileInputStream);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }
        return properties;
    }

    public static Properties getPropertiesInConfigPath(String propertiesFileName) {
        try {
            if (ConfigUtil.mode == Mode.DEVELOP) {
                return load(new File(getClassPathOuterBaseClassLoader().getResource("common/develop/" + propertiesFileName).toURI()));
            } else if (ConfigUtil.mode == Mode.TEST) {
                return load(new File(getClassPathOuterBaseClassLoader().getResource("common/test/" + propertiesFileName).toURI()));
            } else if (ConfigUtil.mode == Mode.PRODUCE) {
                return load(new File(getClassPathOuterBaseClassLoader().getResource("common/produce/" + propertiesFileName).toURI()));
            } else {
                throw new RuntimeException("else");
            }
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据开发模式得到当前的路径
     */
    public static String getConfigPath() {
        String path = null;
        if (ConfigUtil.mode == Mode.DEVELOP) {
            path = getDevelopConfigPath();
        } else if (ConfigUtil.mode == Mode.TEST) {
            path = getTestConfigPath();
        } else if (ConfigUtil.mode == Mode.PRODUCE) {
            path = getProduceConfigPath();
        } else {
            throw new RuntimeException("else");
        }

        return path;
    }

    public static String separator() {
        if (ConfigUtil.separator != null) {
            return ConfigUtil.separator;
        } else {
            String separator = null;
            if (System.getProperty("os.name").toLowerCase().contains("windows")) {
                separator = File.separator;
            } else {
                separator = slash;
            }
            ConfigUtil.separator = separator;
            return separator;
        }

    }

}
